﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace MyAnimeList
{
    public partial class FrmLogIn : Form
    {
        public FrmLogIn()
        {
            InitializeComponent();
        }

        private void FrmLogIn_Load(object sender, EventArgs e)
        {
        }

        private void txtUserName_TextChanged(object sender, EventArgs e)
        {
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUserName.Text) || string.IsNullOrEmpty(txtPassword.Text))
            {
                return;
            }
            var username = txtUserName.Text.Trim();
            var password = txtPassword.Text;

            var message = LibDataProcess.Login(username, password);
            if (message != null)
            {
                MessageBox.Show(message, "Error", MessageBoxButtons.OK);
            }
            else
            {
                // Mã hóa username và password, và lưu lại
                //password =

                if (LibDataProcess.SaveConfig(new Dictionary<string, string>
                {
                    {"UserName", username},
                    {"Password", password}
                }, out message) == false)
                {
                    MessageBox.Show(message, "Error", MessageBoxButtons.OK);
                    return;
                }

                // Đóng form Login
                var form = new FrmMainForm {TaiKhoan = username, MatKhau = password, LoggedIn = true};
                form.BringToFront();
                Dispose();
            }
        }
    }
}